<template>
  <el-dialog :title="'添加主备配置'" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="modelRules" :model="model" label-width="160px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="model.name" placeholder="名称"/>
      </el-form-item>
      <el-form-item label="本机主备类型" prop="type">
        <el-select v-model="model.type" placeholder="请选择本机主备类型" style="display: block">
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="对端IP地址" prop="ip">
        <el-input v-model="model.ip" placeholder="对端IP地址"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close()">取消</el-button>
      <el-button type="primary" :loading="isSubmitLoading" @click="save()">提交</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { creatRedundancy } from "@/api/redundancy";
import pattern from '@/utils/pattern';

export default {
  data() {
    return {
      dialogVisible: false,
      isSubmitLoading: false,
      typeOptions: [
        {
          value: 1,
          label: '主网关'
        },
        {
          value: 2,
          label: '备网关'
        }
      ],
      model: {
        name: undefined,
        ip: undefined,
        type: 1
      },
      modelRules: {
        name: [{ required: true, trigger: "blur", message: "请输入名称" }],
        type: [{ required: true, trigger: "select", message: "请选择本机主备类型" }],
        ip: [
          { required: true, trigger: "blur", message: "请输入对端IP地址" },
          { pattern: pattern.ip, message: "请输入正确的IP地址" }
        ]
      }
    };
  },
  methods: {
    show() {
      this.resetModal();
      this.isSubmitLoading = false;
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    resetModal() {
      this.model = {
        name: undefined,
        ip: undefined,
        type: 1
      };
    },
    save() {
      this.isSubmitLoading = true;
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          creatRedundancy(this.model)
            .then(() => {
              this.$message({ message: "添加成功", type: "success" });
              this.isSubmitLoading = false; // 无论成功失败，都把按钮的loading状态取消
              this.close();
              this.$emit("save");
            })
            .catch(() => {
              this.isSubmitLoading = false; // 无论成功失败，都把按钮的loading状态取消
            });
        } else {
          this.isSubmitLoading = false; // 无论成功失败，都把按钮的loading状态取消
          this.$message({ message: this.$t("formIsNotValidMessage"), type: "warning" });
        }
      });
    },
    close() {
      this.dialogVisible = false;
      this.isSubmitLoading = false; // 无论成功失败，都把按钮的loading状态取消
    }
  }
};
</script>
